﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HFW.Share;
using Janus.Windows.GridEX;

namespace HFW.AssetManagement.Catalogue
{
    public partial class Asset_KhachHang : BaseForm
    {
        public Asset_KhachHang()
        {
            InitializeComponent();
            this.Load += new EventHandler(Khachhang_Load);
            this.filterEditor.FilterConditionChanged += new EventHandler(filterEditor_FilterConditionChanged);
        }

        void filterEditor_FilterConditionChanged(object sender, EventArgs e)
        {
            gridOverView.RootTable.FilterCondition = (IFilterCondition)filterEditor.FilterCondition;

        }

        void Khachhang_Load(object sender, EventArgs e)
        {
            this.fncRefresh();
        }

        private void gridOverView_EditingCell(object sender, EditingCellEventArgs e)
        {
            if (e.Column.Key == "MaKH" && gridOverView.GetRow().RowType != RowType.NewRecord)
                e.Cancel = true;
        }

        private void gridOverView_AddingRecord(object sender, CancelEventArgs e)
        {
            if (DialogResult.Yes == MessageBoxEX.Show("Xác nhận thêm dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                using (var conn = new DataProvider.Connection())
                {
                    try
                    {
                        conn.RunDynamicSQLQuery("udp_Asset_KhachHang_ups",
                                                new object[]
                                                    {
                                                        gridOverView.GetValue("MaKH").ToString().Replace(" ", ""),
                                                        gridOverView.GetValue("TenKH").ToString(),
                                                        gridOverView.GetValue("DiaChi").ToString(),
                                                        gridOverView.GetValue("DienThoai").ToString(),
                                                        gridOverView.GetValue("MST").ToString(),
                                                        gridOverView.GetValue("GhiChu").ToString()
                                                    });
                        fncRefresh();
                        gridOverView.Refetch();
                    }
                    catch (Exception)
                    {
                        MessageBoxEX.Show("Error 15 : Lỗi kết nối cơ sở dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                        e.Cancel = true;
                    }

                }
            }
            else
            {
                e.Cancel = true;
                gridOverView.Refetch();
            }

        }

        private void gridOverView_UpdatingRecord(object sender, CancelEventArgs e)
        {
            if (DialogResult.Yes == MessageBoxEX.Show("Xác nhận thay đổi dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                using (var conn = new HFW.DataProvider.Connection())
                {
                    try
                    {
                        conn.RunDynamicSQLQuery("udp_Asset_KhachHang_ups",
                                                new object[]
                                                    {
                                                        gridOverView.GetValue("MaKH").ToString(),
                                                        gridOverView.GetValue("TenKH").ToString(),
                                                        gridOverView.GetValue("DiaChi").ToString(),
                                                        gridOverView.GetValue("DienThoai").ToString(),
                                                        gridOverView.GetValue("MST").ToString(),
                                                        gridOverView.GetValue("GhiChu").ToString()
                                                    });
                        fncRefresh();
                    }
                    catch (Exception)
                    {
                        MessageBoxEX.Show("Error 16 : Lỗi kết nối cơ sở dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX,
                                   MessageBoxButtons.OK,
                                   MessageBoxIcon.Error);
                        e.Cancel = true;
                    }
                    
                }
            }
            else
            {
                e.Cancel = true;
                gridOverView.Refetch();
            }
        }

        private void gridOverView_DeletingRecord(object sender, RowActionCancelEventArgs e)
        {
            if (DialogResult.Yes == MessageBoxEX.Show("Xác nhận xóa khách hàng '" + gridOverView.GetValue("TenKH") + "'", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                using (var conn = new HFW.DataProvider.Connection())
                {
                    try
                    {
                        object ok = conn.RunDynamicSQLScalar("udp_Asset_KhachHang_del", new object[] { gridOverView.GetValue("MaKH").ToString() });
                        fncRefresh();
                        gridOverView.Refetch();
                        if (!(bool)ok)
                        {
                            MessageBoxEX.Show("Khách hàng này đã có phát sinh.", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            e.Cancel = true;
                        }
                    }
                    catch (Exception)
                    {
                        MessageBoxEX.Show("Error 17 : Lỗi kết nối cơ sở dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX,
                                   MessageBoxButtons.OK,
                                   MessageBoxIcon.Error);
                        e.Cancel = true;
                    }
                }
            }
            else
            {
                e.Cancel = true;
            }
        }

        #region Override Standard ToolBar Methods from Base

        public override void fncSave()
        {
        }

        public override void fncDelete()
        {
        }

        public override void fncEdit()
        {
        }

        public override void fncRefresh()
        {
            using (var conn = new HFW.DataProvider.Connection())
            {
                try
                {
                    var dstemp = new DataSet();
                    dstemp = conn.RunSQLQuery("udp_Asset_KhachHang_lst", CommandType.StoredProcedure);
                    gridOverView.DataSource = dstemp.Tables[0];
                }
                catch (Exception)
                {
                    MessageBoxEX.Show("Error 20 : Lỗi kết nối cơ sở dữ liệu", Applications.APPLICATION_TITLE_MESSAGEBOX,
                                       MessageBoxButtons.OK,
                                       MessageBoxIcon.Error);
                }
                
            }
        }

        public override void fncAdd()
        {
        }

        public override void fncPrint()
        {
        }

        #endregion
    }
}
